package test;

import static org.lwjgl.opengl.GL11.*;

import java.awt.RenderingHints.Key;
import java.util.Random;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.PixelFormat;

class ZCoord
{
	public ZCoord(int x, int y)
	{
		this.x = x;
		this.y = y;
	}
	
	public int x;
	public int y;
}

class ZMap
{
	public ZMap()
	{
		for (int i = 0; i < coord.length; i++)
		{
			int x = new Random().nextInt(5000);
			int y = new Random().nextInt(5000);
			
			this.coord[i] = new ZCoord(x, y);
		}
	}
	
	public void redraw()
	{
		glColor3f(1.0f, 0.0f, 0.0f);
		glBegin(GL_LINE_LOOP);
			glVertex2i(0, 0);
			glVertex2i(5000, 0);
			glVertex2i(5000, 5000);
			glVertex2i(0, 5000);
		glEnd();
		
		glPointSize(5.0f);
		glColor3f(1.0f, 1.0f, 1.0f);
		
		glBegin(GL_POINTS);
			for (int i = 0; i < coord.length; i++)
				glVertex2i(coord[i].x, coord[i].y);
		glEnd();
	}
	
	public void update()
	{
		for (int i = 0; i < coord.length; i++)
		{
			coord[i].x += 10;
			coord[i].y += 10;
		}
	}
	
	ZCoord coord[] = new ZCoord[1000];
}

public class TextureLoadingMain
{
	public void initOpenGL(String windowTitle)
	{
		try
		{
			Display.setDisplayMode(new DisplayMode(800, 600));

			Display.setTitle(windowTitle);
 
			PixelFormat pixelFormat = new PixelFormat();
			ContextAttribs contextAtrributes = new ContextAttribs(1, 0);
			Display.create(pixelFormat, contextAtrributes);
 
			glViewport(0, 0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight());
			glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
			glOrtho(0, Display.getDisplayMode().getWidth(), Display.getDisplayMode().getHeight(), 0, 1, -1);
		}
		catch( LWJGLException lwjglException )
		{
			throw new RuntimeException("Aaargh, could not initiate LWJGL", lwjglException);
		}
	}
 
	public void initAndRun(String windowTitle) throws LWJGLException
	{
		initOpenGL(windowTitle);
		run();
	}
 
	public void run() throws LWJGLException
	{	
		long time;
		
		ZMap map = new ZMap();
		
		int cameraX = 0;
		int cameraY = 0;
		
		float angle	= 1.0f;
		int counter = 0;
		
		while(Display.isCloseRequested() == false)
		{
			if (counter == 10)
			{
				if (angle == 1.0f)
				{
					angle = -1.0f;
				}
				else
				{
					angle = 1.0f;
				}
				
				counter = 0;
				map.update();
			}
			
			counter++;
			
			time = System.currentTimeMillis();
			
			glClear(GL_COLOR_BUFFER_BIT);
			glColor3f(1, 1, 1);
			
			glPushMatrix();
			glMatrixMode(GL_MODELVIEW);
			glRotatef(angle / 5.0f, 0.0f, 0.0f, 1.0f);
			glTranslatef(-cameraX, -cameraY, 0.0f);
			
			if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))	cameraX -= 10;
			if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))	cameraX += 10; 
			if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))	cameraY += 10;
			if (Keyboard.isKeyDown(Keyboard.KEY_UP))	cameraY -= 10;
			
			map.redraw();
			
			glPopMatrix();
			
			time = System.currentTimeMillis();
			Display.update();
		}
		Display.destroy();
	}
 
	public static void main(String[] args) throws LWJGLException
	{
		TextureLoadingMain example = new TextureLoadingMain();
		example.initAndRun("Test");
	}
}

